﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace ImportSubscriber
{
    static class Program
    {
        public static frmMain frm;       

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                //Unhandled Exception
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //UI Exception
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //NON UI Exception
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                frm = new frmMain();
                Application.Run(Log.SetForm(frm));
            }
            catch (Exception ex)
            {
                string str = "Exception: " + DateTime.Now.ToString() + "\r\n";
                if (ex != null)
                    str = str + String.Format("Exception: {0}\r\nMessage: {1}\r\nStack: {2}\r\n", ex.GetType().Name, ex.Message, ex.StackTrace);
                else
                    str = str + String.Format("Exception: {0}", ex);
                writeLog(str);
                MessageBox.Show("Exception, please contact author!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            string str = "ThreadException: " + DateTime.Now.ToString() + "\r\n";
            Exception error = e.Exception as Exception;
            if (error != null)
                str = str + String.Format("ThreadException: {0}\r\nMessage: {1}\r\nStack: {2}\r\n", error.GetType().Name, error.Message, error.StackTrace);
            else
                str = String.Format("ThreadException: {0}", e);
            writeLog(str);
            MessageBox.Show("ThreadException exception, please contact author!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string str = "UnhandledException: " + DateTime.Now.ToString() + "\r\n";
            Exception error = e.ExceptionObject as Exception;
            if (error != null)
                str = str + String.Format("UnhandledException: {0}\n\rStack: {1}\r\n", error.Message, error.StackTrace);
            else
                str = string.Format("UnhandledException:{0}", e);
            writeLog(str);
            MessageBox.Show("Unhandled exception, please contact author!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        static void writeLog(string str)
        {
            if (!Directory.Exists("ErrLog"))
                Directory.CreateDirectory("ErrLog");
            using (StreamWriter sw = new StreamWriter(@"ErrLog\ErrLog_" + DateTime.Now.ToString("yyyy_MM_dd") + ".txt", true))
            {
                sw.WriteLine("---------------------------------------------------------");
                sw.WriteLine(str);
                sw.Close();
            }
        }

    }
}